package com.kaiyidiannao.activity.two_wei_ma;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.req.model.GetSoftVersion;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by 武 on 2014/6/5 0005.
 */
public class TwoWeiMa extends BaseActionBarActivity implements Observer {

    private ImageView two_wei_mas;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two_wei_ma);
        DataCenter.getInstance().addObserver(this);
        showProgressDialog("正在加载");
        new GetSoftVersion("address").execute();
        //处理ActionBar
        findActionBar();
    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("查看二维码");
        two_wei_mas = (ImageView) findViewById(R.id.two_wei_mas);
    }

    public Bitmap Create2DCode(String str) throws WriterException {
        //生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        //二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        closeProgressDialog();

        if (flag == DataCenter.flag.GET_SOFTVERSION_SUCCESS
                || flag == DataCenter.flag.GET_SOFTVERSION_FAILED) { //类表数据

            if (flag == DataCenter.flag.GET_SOFTVERSION_SUCCESS) {
                url = DataCenter.address;
                String urls = url+"#mp.weixin.qq.com";
                try {
                    Log.d("aaaaaaaaaaa",url+"#mp.weixin.qq.com");
                    two_wei_mas.setImageBitmap(Create2DCode(url));
                } catch (WriterException e) {
                    e.printStackTrace();
                }
            }
        }

        if (flag == DataCenter.flag.GET_SOFTVERSION_FAILED) {
            closeProgressDialog();
            //showToast(getString(R.string.no_network));
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, TwoWeiMa.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }


}
